package com.bjpowernode.service.impl;

import com.bjpowernode.mapper.TActivityMapper;
import com.bjpowernode.mapper.TClueMapper;
import com.bjpowernode.mapper.TCustomerMapper;
import com.bjpowernode.mapper.TTranMapper;
import com.bjpowernode.result.NameValueResult;
import com.bjpowernode.result.SummaryResult;
import com.bjpowernode.service.StatisticsService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 统计服务实现类
 * 提供销售数据汇总、销售漏斗分析和线索来源分析等统计功能
 */
@Service
public class StatisticsServiceImpl implements StatisticsService {
    @Resource
    private TActivityMapper tActivityMapper;  // 活动数据访问接口

    @Resource
    private TClueMapper tClueMapper;          // 线索数据访问接口

    @Resource
    private TCustomerMapper tCustomerMapper;  // 客户数据访问接口

    @Resource
    private TTranMapper tTranMapper;          // 交易数据访问接口

    /**
     * 获取销售数据汇总
     * @return 包含各类统计指标的SummaryResult对象
     */
    @Override
    public SummaryResult getSummaryData() {
        //进行中的活动数
        Integer goingActivityCount = tActivityMapper.selectGoingActivityCount();
        //活动总数
        Integer totalActivityCount = tActivityMapper.selectTotalActivityCount();
        //总线索
        Integer totalClueCount = tClueMapper.selectTotalClueCount();
        //总客户
        Integer totalCustomerCount = tCustomerMapper.selectTotalCustomerCount();
        //成功的交易额
        BigDecimal successTranAmount = tTranMapper.selectSuccessTranAmount();
        //总交易额
        BigDecimal totalTranAmount = tTranMapper.selectTotalTranAmount();

        // 构建并返回汇总结果对象
        SummaryResult summaryResult = SummaryResult.builder()
                .goingActivityCount(goingActivityCount)
                .totalActivityCount(totalActivityCount)
                .totalClueCount(totalClueCount)
                .totalCustomerCount(totalCustomerCount)
                .successTranAmount(successTranAmount)
                .totalTranAmount(totalTranAmount)
                .build();

        return summaryResult;
    }

    /**
     * 获取销售漏斗数据
     * 展示从线索到成交的转化路径数据
     * @return 包含各阶段名称和数量的NameValueResult列表
     */
    @Override
    public List<NameValueResult> getSaleFunnel() {
        List<NameValueResult> nameValueResults = new ArrayList<>();
        //线索总数
        Integer totalClueCount = tClueMapper.selectTotalClueCount();
        NameValueResult clueData = new NameValueResult("线索", totalClueCount);
        nameValueResults.add(clueData);

        //客户总数
        Integer customerCount = tCustomerMapper.selectTotalCustomerCount();
        NameValueResult customerData = new NameValueResult("客户", customerCount);
        nameValueResults.add(customerData);

        //交易总数
        Integer totalTranCount = tTranMapper.selectTotalTranCount();
        NameValueResult tranData = new NameValueResult("交易", totalTranCount);
        nameValueResults.add(tranData);

        //交易成功总数
        Integer selectSuccessTranCount = tTranMapper.selectSuccessTranCount();
        NameValueResult successTranData = new NameValueResult("成交", selectSuccessTranCount);
        nameValueResults.add(successTranData);

        return nameValueResults;
    }

    /**
     * 获取线索来源分布数据
     * @return 包含各来源名称和数量的NameValueResult列表
     */
    @Override
    public List<NameValueResult> getCluePie() {
        return tClueMapper.cluePie();
    }
}
